package org.example;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.stereotype.Service;

@Service
public class RedisService {
    //StringRedisTemplate底层默认使用的是Lettuce客户端
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private RedisProperties redisProperties;

    @Autowired
    public RedisService(StringRedisTemplate stringRedisTemplate) {
        this.stringRedisTemplate = stringRedisTemplate;
    }

    public void setKey(String key, String value) {
        stringRedisTemplate.opsForValue().set(key, value);
    }

    public String getValue(String key) {
        return stringRedisTemplate.opsForValue().get(key);
    }

    public int getDatabase() {
        return redisProperties.getDatabase();
    }
}
